﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NLUNZ.Business;

namespace NLUNZ.UserControls
{
    public partial class DeliveryBulkUpload : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 读取Excel文件，将内容存储在DataSet中
        /// </summary>
        /// <param name="opnFileName">带路径的Excel文件名</param>
        /// <returns>DataSet</returns>


        protected void btnOK_Click(object sender, EventArgs e)
        {
            int DelivRecord, err, cor = 0;
            bool isCor = true;
            string st = "";

            string opnFileName = fuExcel.FileName.ToString();


            bool isExcel = Utility.IsExcel(opnFileName);

            if (isExcel == true)
            {            
                //设定上传文件的保存路径
                //string strSaveDir = System.Web.HttpContext.Current.Server.MapPath("~") + "upload\\";
                string path = Server.MapPath("~/Upload/Delivery/");
                string strNewFile = Utility.NewFileName(opnFileName, path);
                fuExcel.SaveAs(strNewFile);


                DataSet ds = Utility.ExcelToDataSet(strNewFile);
                //Response.Write(nluDB.strExcelToDataSet(strNewFile).ToString());
                //Response.Write(strNewFile);
                //DataSet ds = null;
                if (ds.Tables.Count == 0)
                {
                    Literal1.Text = Utility.ErrorHtml("错误,你所上传的文件没有记录.");
                }
                else
                {
                    DataTable dt = ds.Tables[0];
                    Delivery delivery = new Delivery();

                    try
                    {
                        int totalActualRecords = 0;

                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            if (!String.IsNullOrEmpty(dt.Rows[i][0].ToString().Trim()))
                            {
                                totalActualRecords++;
                            }
                        }

                        for (int i = 0; i < totalActualRecords; i++)
                        {
                            DelivRecord = i + 2;

                            //录入运单号,必填
                            delivery.DeliveryNum = dt.Rows[i][0].ToString();

/*                            //设清关日期为空
                            DateTime date;
                            DateTime time;

                            //录入取货日期时间,必填
                            date = DateTime.Parse(dt.Rows[i][1].ToString());
                            time = DateTime.Parse(dt.Rows[i][2].ToString());

                            delivery.ReceivedDateTime = DateTime.Parse(String.Format("{0} {1}", date.ToString("yyyy-MM-dd"),
                                                                       time.ToString("HH:mm")));

                            //录入取件人名字,必填
                            delivery.Recipient = dt.Rows[i][3].ToString();

                            //录入发出城市
                            if (dt.Rows[i][4].ToString() != "")
                            {
                                delivery.DeliveryCity = dt.Rows[i][4].ToString();
                                
                            }*/


 /*                           //录入入库日期时间
                            string inventoryDate;
                            string inventoryTime;
                            if (dt.Rows[i][6].ToString() != "")
                            {
                                inventoryDate = DateTime.Parse(dt.Rows[i][6].ToString()).ToString("yyyy-MM-dd");
                                if (dt.Rows[i][7].ToString() != "")
                                {
                                    inventoryTime = DateTime.Parse(dt.Rows[i][7].ToString()).ToString("HH:mm");
                                    delivery.InventoryDateTime =
                                        DateTime.Parse(string.Format("{0} {1}", inventoryDate, inventoryTime));
                                }
                            }
                            */

                            //录入到达机场日期时间
                            string airportDate;
                            string airportTime;
                            if (dt.Rows[i][1].ToString() != "")
                            {
                                airportDate = DateTime.Parse(dt.Rows[i][1].ToString()).ToString("yyyy-MM-dd");
                                if (dt.Rows[i][2].ToString() != "")
                                {
                                    airportTime = DateTime.Parse(dt.Rows[i][2].ToString()).ToString("HH:mm");
                                    delivery.AirportDateTime =
                                        DateTime.Parse(String.Format("{0} {1}", airportDate, airportTime));
                                }
                            }

                            //录入海关名称
                            if (dt.Rows[i][3].ToString() != "")
                            {
                                delivery.Custom = dt.Rows[i][3].ToString();
                            }

                            //录入前往海关日期时间
                            string deliveryDate;
                            string deliveryTime;
                            if (dt.Rows[i][4].ToString() != "")
                            {
                                deliveryDate = DateTime.Parse(dt.Rows[i][4].ToString()).ToString("yyyy-MM-dd");
                                if(dt.Rows[i][5].ToString() != "")
                                {
                                    deliveryTime = DateTime.Parse(dt.Rows[i][5].ToString()).ToString("HH:mm");
                                    delivery.DeliveryDateTime =
                                        DateTime.Parse(String.Format("{0} {1}", deliveryDate, deliveryTime));
                                }
                            }


                            if(!Repository.UpdateDeliveryInfo(delivery))
                            {
                                err = DelivRecord;
                                st = st + " " + err.ToString();
                                isCor = false;
                            }
                            else
                            {
                                cor++;
                            }

                        }
                        string log = DateTime.UtcNow.AddHours(+12).ToString("yyyy-MM-dd HH:mm:ss") + " 已经将" + cor.ToString() + "条记录写进数据库.";
                        if (isCor == true)
                        {
                            Literal1.Text = Utility.SuccessHtml(log);
                        }
                        else
                        {
                            log = log + "第" + st + " 行数据有误,录入失败.";
                            Literal1.Text = Utility.ErrorHtml(log);
                        }
                    }
                    catch
                    {
                        Literal1.Text = Utility.ErrorHtml("错误,文件数据有误,录入失败.");
                        System.IO.File.Delete(strNewFile);
                    }
                }
            }
            else
            {
                Literal1.Text = Utility.ErrorHtml("错误,只能上传Excel文件.");
            }
        }
        

    }
}